package com.kelvin.backend.common.util;

import org.aspectj.lang.JoinPoint;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * <p>Title: AnnotationUtil</p>
 * <p>Description: </p>
 *
 * @author Kelvin.Chen
 * @datetime 2021/7/9 17:01
 */
public class AnnotationUtil {

    public static Annotation getAnnotation(JoinPoint jp, Class clazz) throws Exception {
        String targetName = jp.getTarget().getClass().getName();
        String methodName = jp.getSignature().getName();
        Object[] args = jp.getArgs();

        Class targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();

        Annotation annotation = null;
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length == args.length) {
                    annotation = method.getAnnotation(clazz);
                    break;
                }
            }
        }

        return annotation;
    }
}
